package com.feicheng.common.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

public class DateUtils {

    private static HashMap<String, DateTimeFormatter> patternMap = new HashMap<>();

    static {
        patternMap.put("yyyyMMdd", DateTimeFormatter.ofPattern("yyyyMMdd"));
        patternMap.put("yyyy-MM-dd", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        patternMap.put("yyyy-MM-dd hh:mm:ss", DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
    }

    public static LocalDate coverToLocalDateByMills(long millisTimestamp) {
        long secondsTimestamp = millisTimestamp / 1000;
        Instant instant = Instant.ofEpochSecond(secondsTimestamp);
        return instant.atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime coverToLocalDateTimeByMills(long millisTimestamp) {
        long secondsTimestamp = millisTimestamp / 1000;
        Instant instant = Instant.ofEpochSecond(secondsTimestamp);
        return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static String dateToString(String pattern, LocalDate localDate) {
        return localDate.format(patternMap.get(pattern));
    }
}
